Opening LIGGGTHS output VTK files in Python

Metadata
aliases: []
shorthands: {}
created: 2022-07-08 16:01:55
modified: 2022-07-08 16:27:24

When we save output files in VTK when using LIGGGHTS, we can later open and analyze these files using Python.

For this, we first need to install the vtk package using pip:

pip3 install vtk

Also numpy is needed, because we will load the files into numpy arrays:

pip3 install numpy

Then we can import them in the Python file along with the vtk_to_numpy function:

import numpy as np
import vtkmodules.all as vtk
from vtkmodules.util.numpy_support import vtk_to_numpy

Now we can create a reader and open the file. As an example:

reader = vtk.vtkPolyDataReader()
reader.SetFileName(f"../outfiles/{foldername}/{filename}")
reader.Update()
output = reader.GetOutput()

# Then retrieve the coordinate, velocity and orientation matrix of each particle
coords = vtk_to_numpy(output.GetPoints().GetData())
vs = vtk_to_numpy(output.GetPointData().GetArray(3))
orientations = vtk_to_numpy(output.GetPointData().GetArray(8))

Depending on the output file, we can also get other properties, like size, angular momentum, etc. If we want to figure out the index of a given property is, we need to print the properties of the reader. We can do it like this:

print(output.GetPointData())

And an example result of this, showing us the names and indices of properties:

...
  Array 0 name = id
  Array 1 name = type
  Array 2 name = mass
  Array 3 name = v
  Array 4 name = omega
  Array 5 name = shapex
  Array 6 name = shapey
  Array 7 name = shapez
  Array 8 name = TENSOR
  Array 9 name = blockiness1
  Array 10 name = blockiness2
...

The TENSOR member is the orthogonal matrix describing the orientation of the particle and the blockiness values are related to superquadrics.